
<html>
<head><title>CS143 Project 1B</title></head>
<body>
<font size = "20">LMDB</font><br>
<i>Limited Movie Database</i><br><br><br>


Type an SQL query in the following box:
<p>
<form method="GET">
<textarea name="query" cols="60" rows="8">
</textarea>
<input type="submit" value="Submit" />
</form>
</p>
<p><small>Note: tables and fields are case sensitive. Run "show tables" to see the list of
available tables.</small>
</p>

<?php

//connect to database
$db_connection = mysql_connect("localhost", "cs143", "");

if(!$db_connection) {
    $errmsg = mysql_error($db_connection);
    print "Connection failed: ".$errmsg;
    exit(1);
}

mysql_select_db("CS143", $db_connection);

//send query
$query = $_GET["query"];

$result = mysql_query($query, $db_connection);

if(!$result) {
   exit(1);}


echo "<h3>Results from MySQL:</h3>";
echo "<table border='2'><tr>";

if ($result <> '\N'){
$fields_num = mysql_num_fields($result);}


// print table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td><b>{$field->name}</b></td>";
}

echo "</tr>\n";

// print table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}


mysql_close($db_connection);

?>


</body>
</html>
